<template>
    <div v-if="visible" class="dialog-overlay">
        <div class="dialog">
            <!-- 对话框头部 -->
            <div class="dialog-header">
                <h3>添加实体定义</h3>
                <button @click="closeDialog" class="close-button">×</button>
            </div>

            <!-- 表单主体 -->
            <div class="dialog-body">
                <form @submit.prevent="confirm">
                    <div class="form-item">
                        <label>ID</label>
                        <input v-model="entity.id" type="text" placeholder="请输入实体ID" />
                    </div>
                    <div class="form-item">
                        <label>名称</label>
                        <input v-model="entity.name" type="text" placeholder="请输入实体名称" />
                    </div>
                    <div class="form-item">
                        <label>描述</label>
                        <input v-model="entity.description" type="text" placeholder="请输入实体描述" />
                    </div>
                    <div class="form-item">
                        <label>路径</label>
                        <input v-model="entity.path" type="text" placeholder="请输入实体路径" />
                    </div>
                    <div class="form-item">
                        <label>属性</label>
                        <button type="button" @click="addAttribute" class="attribute-button">添加属性</button>
                    </div>
                    <div v-for="(attribute, index) in entity.attributes" :key="index" class="form-item attribute-item">
                        <input v-model="attribute.key" type="text" placeholder="属性名" />
                        <select v-model="attribute.type">
                            <option value="text">文本</option>
                            <option value="number">数字</option>
                            <option value="boolean">布尔值</option>
                            <option value="array">数组</option>
                            <option value="object">对象</option>
                        </select>
                        <input v-model="attribute.description" type="text" placeholder="描述" />
                        <button type="button" @click="removeAttribute(index)" class="remove-button">删除</button>
                    </div>
                    <div class="dialog-footer">
                        <button type="button" @click="closeDialog" class="cancel-button">取消</button>
                        <button type="submit" class="primary-button">确定</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</template>

<script setup>
import { defineProps, computed, getCurrentInstance, reactive, nextTick } from 'vue';

const { emit } = getCurrentInstance();

// 响应式数据
const entity = reactive({
    id: '',
    name: '',
    description: '',
    path: '',
    attributes: []
});

// 对话框显示控制
const props = defineProps({ modelValue: Boolean });

const visible = computed({
    get: () => props.modelValue,
    set: async (value) => {
        emit('update:modelValue', value);
        await nextTick(); // 确保DOM更新完成
    }
});

// 操作方法
const closeDialog = () => {
    visible.value = false;
    Object.assign(entity, { id: '', name: '', description: '', path: '', attributes: [] });
};

const addAttribute = () => {
    entity.attributes.push({ key: '', type: 'text', description: '' });
};

const removeAttribute = (index) => {
    entity.attributes.splice(index, 1);
};

const confirm = () => {
    // 将属性数组转换为JSON字符串
    const entityToSave = {
        id: entity.id,
        text: entity.name,
        description: entity.description,
        path: entity.path,
        properties: JSON.stringify(entity.attributes)
    };
    emit('confirm', entityToSave);
    closeDialog();
};
</script>

<style scoped>
/* 蒙层样式 */
.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

/* 对话框容器 */
.dialog {
    background: white;
    width: 60%;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* 头部样式 */
.dialog-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    background: #f5f5f5;
}

.dialog-header h3 {
    margin: 0;
    font-size: 18px;
    color: rgba(0, 0, 0, 0.85);
}

.close-button {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
}

/* 表单内容区域 */
.dialog-body {
    padding: 20px;
}

.form-item {
    margin-bottom: 16px;
    display: flex;
    align-items: center;
}

.form-item label {
    margin-right: 10px;
    font-weight: bold;
    width: 80px;
    /* 固定宽度以对齐 */
}

.attribute-item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}

.attribute-item input,
.attribute-item select {
    margin-right: 10px;
    flex: 1;
}

/* 底部操作区 */
.dialog-footer {
    display: flex;
    justify-content: flex-end;
    padding: 15px;
    background: #f5f5f5;
}

/* 按钮样式 */
.primary-button {
    background-color: #409eff;
    color: white;
    border: none;
    padding: 8px 16px;
    cursor: pointer;
    border-radius: 4px;
    margin-left: 10px;
}

.primary-button:hover {
    background-color: #66b1ff;
}

.cancel-button {
    background-color: #f0f0f0;
    color: #333;
    border: none;
    padding: 8px 16px;
    cursor: pointer;
    border-radius: 4px;
}

.cancel-button:hover {
    background-color: #e0e0e0;
}

.attribute-button {
    background-color: #fff;
    color: #409eff;
    border: 1px solid #409eff;
    padding: 5px 10px;
    cursor: pointer;
    border-radius: 4px;
    margin-left: 10px;
}

.attribute-button:hover {
    background-color: #f0f8ff;
}

.remove-button {
    background-color: #ff4d4f;
    color: white;
    border: none;
    padding: 5px 10px;
    cursor: pointer;
    border-radius: 4px;
}

.remove-button:hover {
    background-color: #ff7875;
}
</style>
